<template>
    <div>
        <home-header></home-header>
        <home-swiper :list='swiperList'></home-swiper>
        <home-icons :list='iconList'></home-icons>
        <home-recommend :list='recommendList'></home-recommend>
        <home-weekend :list='weekendList'></home-weekend>
    </div>
</template>
<script>
import HomeHeader from './components/Header.vue'
import HomeSwiper from './components/swiperList.vue'
import HomeIcons from './components/icons.vue'
import HomeRecommend from './components/recommendList.vue'
import HomeWeekend from './components/weekendList.vue'
import axios from 'axios'
import { mapState } from 'vuex'
export default{
  name: 'Home',
  components: {
    HomeHeader,
    HomeSwiper,
    HomeIcons,
    HomeRecommend,
    HomeWeekend
  },
  data () { // 存储页面数据
    return {
      lastCity: '',
      swiperList: [],
      iconList: [],
      recommendList: [],
      weekendList: [] // 空数组 把swiperList传给home-swiper子组件,在swiper中子组件中接受swiperList数据
    }
  },
  computed: {
    ...mapState(['city'])
  },
  methods: {
    getHomeInfo () {
      axios.get('/api/index.json?city=' + this.city).then(this.getHomeInfoSucc)
    },
    getHomeInfoSucc (res) {
      console.log(res)
      // 响应回数据
      res = res.data
      if (res.ret && res.data) {
        const data = res.data // data.city传值给data 对象中的city ，然后再传值给子组件中的:city='city'，子组件接受了city，header.vue，里的city发生了变化，子组件就会跟着变
        this.swiperList = data.swiperList
        this.iconList = data.iconList
        this.recommendList = data.recommendList
        this.weekendList = data.weekendList
      }
    }
  },
  mounted () {
    this.lastCity = this.city
    this.getHomeInfo()
  },
  activated () { // 作用于页面再次加载，请求被再次激活 挂载后判断 若不是当前的城市 则被再次激活 this.getHomeInfo
    if (this.lastCity !== this.city) {
      this.lastCity = this.city
      this.getHomeInfo()
    }
  // console.log('activated')
  }
}
</script>
<style>

</style>
